package com.ccx.coachsite.bus;

import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.http.UrlConfig;
import com.ccx.coachsite.task.ExpertExInfoTask;
import com.ccx.coachsite.task.ExpertInfoTask;
import com.ccx.coachsite.task.OwerInfoTask;
import com.ccx.coachsite.task.ShareDataTask;
import com.ccx.coachsite.task.TaskType;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.util.StringUtil;

import android.content.Context;

public class LoadDataBus extends BaseBus {

	private int mNeedLoadType;
	private int mLoadedType;
	private int mSuccessLoadedType;
	private ILoadBus mSuperBus;

	public LoadDataBus(Context context, ILoadBus superBus) {
		super(context);
		mNeedLoadType = 0;
		mLoadedType = 0;
		mSuccessLoadedType = 0;
		mSuperBus = superBus;
		
		
	}

	public void preLoadData(String mobile) {
		
		// 我的好友列表
		String firendUrl = UrlConfig.getUrlByKey(UrlConfig.OwerFriendUrlKey);
		firendUrl += "?phone=" + mobile;
		ShareDataTask owerFriendTask = new ShareDataTask(null, mContext,
				firendUrl, SharedKey.Shared_Ower_Friend_List);
		owerFriendTask.execute();

		// 我的收藏列表
		String collectUrl = UrlConfig.getUrlByKey(UrlConfig.OwerCollectUrlKey);
		collectUrl += "?phone=" + mobile;
		ShareDataTask owerCollectTask = new ShareDataTask(null, mContext,
				collectUrl, SharedKey.Shared_Ower_Collect_List);
		owerCollectTask.execute();

		// 我的分享
		String shareUrl = UrlConfig.getUrlByKey(UrlConfig.OwerShareUrlKey);
		shareUrl += "?phone=" + mobile;
		ShareDataTask owerShareTask = new ShareDataTask(null, mContext,
				shareUrl, SharedKey.Shared_Ower_Share_List);
		owerShareTask.execute();

		// 我的消息
		String messageUrl = UrlConfig.getUrlByKey(UrlConfig.OwerMessageUrlKey);
		messageUrl += "?phone=" + mobile;
		ShareDataTask owerMessageTask = new ShareDataTask(null, mContext,
				messageUrl, SharedKey.Shared_Ower_Message_List);
		owerMessageTask.execute();
	}

	public void loadData(String mobile) {
		Account account = AccountMgr.readAccountInfo(mContext);

		OwerInfoTask owerTask = new OwerInfoTask(this, mContext, account);
		owerTask.execute();
		mNeedLoadType += owerTask.getTaskType();

		ExpertInfoTask expertTask = new ExpertInfoTask(this, mContext, account);
		expertTask.execute();
		
		ExpertExInfoTask eTask = new ExpertExInfoTask(this, mContext);
		eTask.execute();
		mNeedLoadType += eTask.getTaskType();
		
		/*
		boolean isFristLogin = LoginHelper.isFirstLogin(mContext);
		if(isFristLogin)
		{
			LoginHelper.setCurrentLogined(mContext);
		}
		*/
		//mNeedLoadType += expertTask.getTaskType();
		
		//preLoadData(mobile);
	}

	@Override
	public void callBack(int taskType, BaseResp response, String responseJson) {
		/*
		if(TaskType.TaskTypeOwerInfo == taskType)
		{
			boolean isSuf = response.isSuccess();
			String desc = "数据加载成功。";
			if(!isSuf )
			{
				if(StringUtil.isEmpty(response.getDesc()))
					desc = "数据加载失败，请检查您的网络是否正常。";
				else
					desc = response.getDesc();
			}
			if(mSuperBus != null)
				mSuperBus.onCompleted(isSuf, desc);
		}
		*/
		try
		{
			if(TaskType.TaskTypeOwerInfo == taskType || TaskType.TaskTypeExpertExInfo == taskType)
			{
				mLoadedType += taskType;
				//if(response.isSuccess())
				mSuccessLoadedType += taskType;
				if (mLoadedType >= mNeedLoadType ) {
					if (mSuperBus != null) {
						boolean isSuf = mSuccessLoadedType >= mNeedLoadType;
						String desc = "数据加载成功。";
						if(!isSuf )
						{
							if(StringUtil.isEmpty(response.getDesc()))
								desc = "数据加载失败，请检查您的网络是否正常。";
							else
								desc = response.getDesc();
						}
						
						mSuperBus.onCompleted(isSuf, desc);
					}
				}
			}
		}
		catch(Exception e)
		{
			
		}
		
	}
}
